import java.sql.*;

public class DBview {
	
	public String[][] tableview(String schema,String sql){
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSetMetaData rsmd = null;
		String[][] dblist = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://10.211.55.6:3306/"+schema,"root","root");
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			rsmd = rs.getMetaData();
			int columncount = rsmd.getColumnCount();
			rs.last();
			int rowcount = rs.getRow();
			rs.first();
			dblist = new String[rowcount+1][columncount];
			
			for(int i=0; i<columncount; i++){
				dblist[0][i] = rsmd.getColumnName(i+1);
			}
			for(int i=1;i<rowcount+1;i++){
				for(int j=0;j<columncount;j++){
					dblist[i][j] = rs.getString(j+1);
				}
				rs.next();
			}
			rs.close();
			pstmt.close();
			con.close();
		} catch (ClassNotFoundException c) {
			// TODO Auto-generated catch block
			System.out.println("Driver error : " + c.getMessage());
		} catch (SQLException s) {
			// TODO Auto-generated catch block
			System.out.println("Query error : " + s.getMessage());
		}		
		return dblist;
	}
	public String[] schemaview(String schema){
		Connection con = null;
		ResultSet rs = null;
		String[] dblist = null;
		DatabaseMetaData dbmd = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://10.211.55.6:3306/"+schema,"root","root");
			dbmd = con.getMetaData();
			rs = dbmd.getTables(null, null, "%", null);
			rs.last();
			int rowcount = rs.getRow();
			rs.first();
			dblist = new String[rowcount];
			for(int i=0;i<rowcount;i++){
				dblist[i]= rs.getString(3);
				rs.next();
			}
			rs.close();
			con.close();
		} catch (ClassNotFoundException c) {
			// TODO Auto-generated catch block
			System.out.println("Driver error : " + c.getMessage());
		} catch (SQLException s) {
			// TODO Auto-generated catch block
			System.out.println("Query error : " + s.getMessage());
		}		
		return dblist;
	}
}
